package mt.dummy;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
 * Creates the table used in MicroTraderClientDummy, using the java class AbstractTableModel
 */

public class ClientTableModel extends AbstractTableModel{	

	ArrayList<Object[]> al;
	String[] header;	

	ClientTableModel(Object[][] obj, String[] header) {		

		this.header = header;
		al = new ArrayList<Object[]>();
		for(int i = 0; i < obj.length; ++i)
				al.add(obj[i]);
	}	

	public int getRowCount() {
		return al.size();
	}

	public int getColumnCount() {
		return header.length;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return al.get(rowIndex)[columnIndex];
		
	}

	public String getColumnName(int index) {
		return header[index];
	}

	void add(String stock, String numberOfUnits, String pricePerUnit, boolean buyOrder) {
		boolean foundStock = false;
		for(int i = 0; i<al.size();i++){			
			if (al.get(i)[0].equals(stock)){
				foundStock = true;
				if (buyOrder)
					al.get(i)[1]= String.valueOf((Integer.parseInt((String) al.get(i)[1]))-Integer.parseInt(numberOfUnits));
				if (!buyOrder)
					al.get(i)[1]= String.valueOf((Integer.parseInt((String) al.get(i)[1]))+Integer.parseInt(numberOfUnits));
				if (Double.parseDouble((String)al.get(i)[1]) == 0){
					al.remove(i);
				}
			}
		}
		if(!foundStock){
		String[] str = new String[3];
		str[0] = stock;
		str[1] = numberOfUnits;
		str[2] = pricePerUnit;
		al.add(str);
		}
		// inform the GUI that I have changed the list information.
		fireTableDataChanged();
		
	}
}